{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "['花', '花开', '花']\n",
      "['花开']\n",
      "['年', '年', '年', '年', '年', '年']\n",
      "['年年', '年年']\n",
      "['年年', '年年']\n",
      "['年', '年', '年年', '年年']\n",
      "['年花落颜色改，明年花开复谁在？年年岁岁花相似，岁岁年年人不同。']\n",
      "['年花', '年花', '年年岁', '年年人']\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "findall() missing 1 required positional argument: 'string'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-093a326e283d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfindall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'年+.'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;31m#以\"年\"开头，以\"花\"结尾，且中间可以有任意多个字符\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfindall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'年'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: findall() missing 1 required positional argument: 'string'"
     ]
    }
   ],
   "source": [
    "import re\n",
    "text=\"今年花落颜色改，明年花开复谁在？年年岁岁花相似，岁岁年年人不同。\"\n",
    "#以“花”为开头，“开”重复0次或1次\n",
    "print(re.findall('花开？',text))\n",
    "#以“花”为开头，“开”重复0次或多次\n",
    "print(re.findall('花开*',text))\n",
    "#以“花”为开头，“开”重复1次或多次\n",
    "print(re.findall('花开+',text))\n",
    "print(re.findall('年{1}',text))     #\"年\"重复1次\n",
    "print(re.findall('年{2}',text))     #\"年\"重复2次\n",
    "print(re.findall('年{2,}',text))    #\"年\"至少重复2次\n",
    "#\"年\"至少重复1次，至多重复2次\n",
    "print(re.findall('年{1,2}',text))\n",
    "#以\"年\"开头，后面可以跟任意多个字符\n",
    "print(re.findall('年.+',text))\n",
    "#\"年\"可以重复1次或多次，后面跟任意字符\n",
    "print(re.findall('年+.',text))\n",
    "#以\"年\"开头，以\"花\"结尾，且中间可以有任意多个字符\n",
    "print(re.findall('年.+花',text))\n",
    "#以\"年\"开头，以\"花\"结尾，且中间可以有任意字符至多一个\n",
    "print(re.findall('年.?花',text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
